<?php
namespace App\Controllers;
class Hockey extends BaseController
{
    // public function index()
    // {
    //     // connect to the model
    //     $places = new \App\Models\Places();
    //     // retrieve all the records
    //     $records = $places->findAll();

    //     $table = new \CodeIgniter\View\Table();

    //     $headings = $places->fields;

    //     $displayHeadings = array_slice($headings, 1, 3);

    //     $table->setHeading(array_map('ucfirst', $displayHeadings));

    //     foreach ($records as $record) {
    //         $nameLink = anchor("travel/showme/$record->id", $record->name);
    //         $table->addRow($nameLink, $record->city,'<img src="/image/' . $record->image . '" alt="' . $record->name . '" style="width:200px; height:auto;">');
    //     }

    //     $template = [
    //         'table_open' => '<table cellpadding="5px">',
    //         'cell_start' => '<td style="border: 1px solid #dddddd;">',
    //         'row_alt_start' => '<tr style="background-color:#dddddd">',
    //     ];
    //     $table->setTemplate($template);

    //     $fields = [
    //         'title' => 'Travel Destinations',
    //         'heading' => 'A List of Travel Destinations',
    //         'footer' => 'Copyright xieyuhang'
    //     ];



    //     //return $table->generate();

    //     // get a template parser
    //     $parser = \Config\Services::parser();
    //     // tell it about the substitions
    //     return $parser->setData($fields)
    //         ->render('templates\top') .
    //         $table->generate() .
    //         $parser->setData($fields)
    //             ->render('templates\bottom');

    // }
    public function index()
    {
        // Connect to the model and retrieve all the records
        $places = new \App\Models\Players();
        $records = $places->findAll();

        // Prepare data for view
        $data['title'] = 'Travel Destinations';
        $data['heading'] = 'A List of Travel Destinations';
        $data['footer'] = 'Copyright xieyuhang';
        $data['records'] = $records;

        // Load the view with the prepared data
        return view('hockey/index', $data);
    }
    public function showme($id)
    {
        $places = new \App\Models\Players();
        $record = $places->find($id);

        if (!$record) {
            throw new \CodeIgniter\Exceptions\PageNotFoundException("No travel destination found with id: $id");
        }

        $table = new \CodeIgniter\View\Table();

        // 设置表头  
        $table->setHeading('Field', 'Value');

        // 添加记录信息  
        $table->addRow('Id', $record['id']);
        $table->addRow('Name', $record['name']);
        $table->addRow('City', $record['city']);
        $table->addRow('powers', $record['powers']);
        $table->addRow('weapon', $record['weapon']);
        //$imageHTML = !empty($record['image']) ?  : 'No image available';  alt="' . $record['name'] . '" style="width:200px; height:auto;
        $table->addRow('Image', '<img src="/image/' . $record['image'] . '" >');



        // 设置表格样式  
        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);

        // 数据模板准备  
        $fields = [
            'title' => $record['name'],
            'heading' => $record['name'],
            'footer' => 'Copyright xieyuhang',
        ];

        // 使用解析器返回结合的视图  
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
            ->render('templates/top') .
            $table->generate() .
            $parser->setData($fields)
                ->render('templates/bottom');
    }
}

